﻿using System;
using System.Windows.Forms;
using RSPJ.Struct;
using RSPJ.Function;
using System.IO;

namespace RSPJ.Windows
{
    public partial class ConvertData : Form
    {
        public Files file;
        string type;

        public ConvertData(Files file)
        {
            InitializeComponent();
            this.file = file;
            type = file.Head.Interleave;
        }

        private void btnChoose_Click(object sender, EventArgs e)
        {
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.Filter = "All Files (*.*)|*.*";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    txtFile.Text = file.Path = sfd.FileName;
                    file.Name = Path.GetFileName(sfd.FileName);
                }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if (rbtnFile.Checked)
            {
                if (rbtnBSQ.Checked) file.Head.Interleave = rbtnBSQ.Text;
                else if (rbtnBIL.Checked) file.Head.Interleave = rbtnBIL.Text;
                else file.Head.Interleave = rbtnBIP.Text;
                funcRW rW = new funcRW();
                rW.write_imagefile(file, txtFile.Text);
                file.Path = txtFile.Text;
                for (int j = 0; j < GlobalData.fileList.Count; j++)
                {
                    if (file.Name == GlobalData.fileList[j].Name)
                    {
                        int tmp = 1, k = 0;
                        string filename;
                        do
                        {
                            filename = file.Name + "(" + tmp + ")";
                            for (k = 0; k < GlobalData.fileList.Count; k++)
                            {
                                if (filename == GlobalData.fileList[k].Name)
                                {
                                    tmp++;
                                    break;
                                }
                            }
                        } while (k != GlobalData.fileList.Count);
                        file.Name = filename;
                    }
                }
                DialogResult = DialogResult.OK;
                Close();
            }
            else
            {
                GlobalData.memory++;
                file.Path = "Memory";
                file.Head.Interleave = "bsq";
                file.Name = "Memory" + GlobalData.memory + ":(" + file.Name + ")";
                DialogResult = DialogResult.OK;
                Close();
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void rbtnBSQ_CheckedChanged(object sender, EventArgs e)
        {
            if(rbtnBSQ.Checked)
            {
                rbtnBIL.Checked = false;
                rbtnBIP.Checked = false;
            }
        }

        private void rbtnBIL_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnBIL.Checked)
            {
                rbtnBSQ.Checked = false;
                rbtnBIP.Checked = false;
            }
        }

        private void rbtnBIP_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnBIP.Checked)
            {
                rbtnBSQ.Checked = false;
                rbtnBIL.Checked = false;
            }
        }

        private void rbtnFile_CheckedChanged(object sender, EventArgs e)
        {
            if (rbtnFile.Checked)
            {
                rbtnMemory.Checked = false;
                panel1.Enabled = true;
                panel1.Visible = true;
            }
            else
            {
                rbtnMemory.Checked = true;
                panel1.Enabled = false;
                panel1.Visible = false;
            }
        }

        private void rbtnMemory_CheckedChanged(object sender, EventArgs e)
        {
            rbtnFile_CheckedChanged(sender, e);
        }
    }
}
